home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1996 April / MacFormat CD Edition MF36 (April 1996).iso / Shareware City / Developers / Tools Plus - GUI⁄Event libs / Tools Plus 2.6.1a Evaluat'n Kit / Tools Plus 2.6.1a / Tutorials / 4-Editing Fields / About… Editing Fields next >
Encoding:
INI File  |  1995-08-01  |  9.9 KB  |  201 lines  |  [TEXT/ttxt]

  1. [Display in Geneva 12pt]
  2.  
  3.  
  4.  
  5. Editing Fields
  6. ~~~~~~~~~~~
  7. This tutorial shows you how to:
  8.    • create a working Edit menu that automatically edits the active
  9.       field’s text
  10.    • create editing fields with different fonts
  11.    • create an editing field that is limited to a certain number
  12.       of characters
  13.    • respond to Tools Plus events pertaining to editing fields
  14.    • put text into a field under application control
  15.    • get text from a field under application control
  16.  
  17. When you create an editing field, Tools Plus executes thousands of lines of code to make it look and feel the way it should. You can easily create professional fields where other developers (without Tools Plus) settle for “good enough” and just use the basics of the Macintosh toolbox.
  18.  
  19. When using Editing Fields, take particular notice of these automatic features (you don’t have to do anything to get these features)…
  20.    • the cursor changes to an I-beam when entering an editing field
  21.    • the Edit menu automatically applies itself to the active field
  22.    • automatic undo/redo (preserving your character selection)
  23.    • text scrolling is a significant improvement over the toolbox’s
  24.       build-in scrolling (it “feels better” by scrolling faster as you
  25.       move your cursor further out of the field)
  26.    • cursor control keys work as you’d expect in editing fields
  27.    • the Shift key extends and shortens your selection when used
  28.       in conjunction with cursor keys
  29.    • the Option key, when used with cursor keys, moves you to the
  30.       beginning of the next/previous word (Option = move by word)
  31.    • the Shift key can be used in conjunction with the Option key to
  32.       extend/shorten your selection a word at a time.
  33.  
  34.  
  35.  
  36.  
  37.  
  38. Editing Menu
  39. ~~~~~~~~~~
  40. To make the most of editing fields, your application should have an Edit menu with the first six items as follows:
  41.     1)       Undo    Cmd Z
  42.     2)       ------------
  43.     3)       Cut      Cmd X
  44.     4)       Copy    Cmd C
  45.     5)       Paste   Cmd V
  46.     6)       Clear
  47.  
  48. Tools Plus takes care of enabling and disabling the individual items based on the user’s activity in the active editing field.
  49.  
  50. The “Undo” item works as you would expect: undoing the user’s last change. The “Undo…” item’s text changes to indicate what will be undone, such as:
  51.     Undo Cut
  52.     Undo Copy
  53.     Undo Paste
  54.     Undo Typing
  55. and so on. What may come as a pleasant surprise is that Undo _also_ works as a Redo… command. For example, when the user starts typing in a field, the Undo item is automatically renamed to “Undo Typing”. If the user uses the Edit menu’s “Undo Typing” item, the typing is undone and the menu changes to “Redo Typing” thereby letting the user undo the undoing. This also applies to the Cut, Copy, Paste and Clear operations.
  56.  
  57. You don’t have to worry about having enough memory to undo or redo, since this is all handled by Tools Plus.
  58.  
  59.  
  60.  
  61.  
  62. How Fields Work
  63. ~~~~~~~~~~~~~
  64. Each editing field needs to be “connected to” its text (supplied by your application when creating the field). Both your application and the editing field will access this text.
  65.  
  66. When your application activates a field, Tools Plus makes a _copy_ of the text you provided and lets the user edit the _copy_ (not the original text). When the user is finished editing the field (by tabbing or clicking to another field, etc), your application decides if the edited text should be discarded or saved (overwriting the original text). Here is an example:
  67.         --------------------------------------------------------
  68.    (1) Field is inactive:                         [Field #1]
  69.                                                              /
  70.                  App’s text: “My dog has fleas”
  71.         --------------------------------------------------------
  72.    (2) Field is activated:                      [Field #1]
  73.                                                              /        \
  74.                  App’s text: “My dog has fleas”         \
  75.                                                                           \
  76.                                                        User’s COPY: “My dog has fleas”
  77.         --------------------------------------------------------
  78.    (3) User Edits the Field:                   [Field #1]
  79.                                                              /        \
  80.                  App’s text: “My dog has fleas”         \
  81.                                                                           \
  82.                                                        User’s COPY: “See spot run!”
  83.         --------------------------------------------------------
  84.    (4) User clicks on another field:
  85.            • your application edits the text and determines there
  86.               are no errors
  87.            • your application SAVES the edited text (overwriting
  88.               the original text)
  89.                                                            [Field #1]
  90.                                                              /        \
  91.                       App’s text: “See spot run!”         \
  92.                                                                           \
  93.                                                        User’s COPY: “See spot run!”
  94.         --------------------------------------------------------
  95.    (5) Your application processes the click in an inactive field.
  96.          The old field (#1) is deactivated and the clicked field is
  97.          activated:
  98.                                                            [Field #1]
  99.                                                              /
  100.                       App’s text: “See spot run!”
  101.         --------------------------------------------------------
  102.  
  103. When you are writing your application, _you_ decide when you want to edit a field’s text and when you want to save the edited text. As an alternative, you may want to perform all editing only if the user clicks the “OK” button instead of on a field-by-field basis. It’s completely up to you.
  104.  
  105.  
  106.  
  107.  
  108.  
  109. Before You Create an Editing Field
  110. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  111. Before you create an editing field, you’ll need to create an object that holds the field’s text. Tools Plus’ editing fields expect a handle that points to a Pascal string.
  112.  
  113. You can create this record yourself any way you want. Tools Plus includes the NewStrHandle routine that allocates space and initializes the string to “” (null string, length = 0). NewStrHandle is handy because it lets you allocate a string of a specific length and initialize it to null with one line of code.
  114.  
  115. Each editing field MUST have its own text handle. It’s okay if two fields share the same text handle providing the two fields do not exist at the same time.
  116.  
  117. Why a “handle” to a string?…
  118.   √ handles reduce memory fragmentation
  119.   √ Tools Plus uses less memory by making a copy of your text only
  120.       when needed. When inactive fields are refreshed by Tools Plus,
  121.       they display your text (by dereferencing your handle) without
  122.       having to keep a copy of the text hanging around.
  123.   √ Tools Plus can optionally resize the handle to be the size of the
  124.       text it contains, making T+ even more memory-efficient!
  125.   √ Tools Plus will soon work with large text fields up to 32K, which
  126.       are best referenced by handle to reduce memory fragmentation
  127.  
  128.  
  129.  
  130.  
  131.  
  132. Creating Editing Fields
  133. ~~~~~~~~~~~~~~~~~~
  134. To create an editing field, you do the following:
  135.    (1) CurrentWindow…     tell Tools Plus which window is the target
  136.                                         of subsequent actions (i.e., the current
  137.                                         window)
  138.    (2) NewField…               create the new field
  139.  
  140. The “Editing Fields” chapter details the parameters required by the NewField or NewFieldRect routines.
  141.  
  142.  
  143.  
  144.  
  145.  
  146. Activating Editing Fields
  147. ~~~~~~~~~~~~~~~~~~~~
  148. Your application can activate a field by using ActivateField. This typically happens when you create a window with new fields. You can also activate a field in response to the user’s action:
  149.     user Tabs or Shift/Tabs…    ActivateField
  150.     user clicks in a field…         ClickInField
  151.  
  152.  
  153.  
  154.  
  155.  
  156. The Event Loop
  157. ~~~~~~~~~~~~
  158. The Tools Plus events you will likely need to handle are:
  159.   doKeyDown, doAutoKey     Detect Tab and Shift/Tab
  160.   doClickField                     User clicked in an inactive field
  161.  
  162. A less commonly used event is doChgInField which tells you something occurred in the field that changed the text (it could be any typing, editing, undoing or redoing). Here’s an example of when the doChgInField event is useful:
  163.    - creating a “log on” window with a User Name and a Password
  164.       field (both fields are empty)
  165.    - create an enabled “Cancel” button, and a disabled “OK” button
  166.    - you want the “OK” button to be enabled only when both fields are
  167.       not empty
  168.    - each time you get a doChgInField event, do the following:
  169.         EnableField(okButton, (!FieldIsEmpty(userNameField)) &&
  170.                             (!FieldIsEmpty(passwordField)));
  171.       or for Pascal…
  172.         EnableField(okButton, (not FieldIsEmpty(userNameField)) and
  173.                             (not FieldIsEmpty(passwordField)));
  174.  
  175. The “OK”  button becomes correctly enabled or disabled with one line of very readable code.
  176.  
  177.  
  178.  
  179.  
  180.  
  181. Frequently Asked Questions
  182. ~~~~~~~~~~~~~~~~~~~~~
  183. Q: How to I put text into a field?
  184. A: There are two ways you can do it…
  185.     [1] Populate the field’s text handle with a Pascal string before
  186.          the field is created, or
  187.     [2] Use PasteIntoField to paste a string into a field after the
  188.          field has been created.
  189.     Of course, the user can type in the field or paste into it by using
  190.     the Edit menu.
  191.  
  192. Q: How do I get text from a field?
  193. A: You can always access the field’s text handle directly. You can
  194.     copy its contents to a Str255 structure, or dereference the handle
  195.     and use it as an Str255 structure. Make sure you only READ from
  196.     the text handle and that you do not manually change.
  197.        If the field is active, you may want to get a copy of the field’s
  198.     EDITED text (not the text that has been saved to the field’s
  199.     handle). Use GetFieldString to obtain a copy of the text being
  200.     edited by the user.
  201.